/**
 * jswf 0.3.2
 * $Rev: 6 $, $Date: 2008-10-10 14:05:38 +0000 (Fri, 10 Oct 2008) $
 *
 * @copyright Copyright (c) 2008 Alexey Yu. Chuprina <656b61@gmail.com>
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

(function(){

var _default = {
	requiredVersion: [9 , 0, 115],
	alternative: 'You need to be updated version of <a href="http://www.macromedia.com/go/getflash/">Flash Player</a>',
	params: {
		menu: 'false',
		wmode: 'transparent'
	}
};

var jswf = window.jswf = function(src, width, height) {
	if (!(this instanceof jswf)) return new jswf(src, width, height);
	this.__construct(src, width, height);
};
jswf.prototype = {
	_id: null,
	_src: null,
	_width: null,
	_height: null,
	_chechVersion: true,
	_requiredVersion: null,
	_alternative: null,
	_params: {},
	_variables: {},
	_class: [],

	__construct: function(src, width, height)
	{
		this._id = 'jswf-' + (Math.round(Math.random()*100000));
		this._src = src;
		this._width = width;
		this._height = height;
		this._requiredVersion = _default.requiredVersion;
		this._alternative = _default.alternative;
		for (var i in _default.params)
			this._params[i] = _default.params[i];
	},

	_buildAlternative: function()
	{
		return ['<div style="width:', this._width, ';height:', this._height, ';">',
			this._alternative,
			'</div>'].join('');
	},

	_buildEmbed: function()
	{
		var i, html = [], fv = [];
		for (i in this._variables)
			fv.push(i + '=' + _urlEncode(this._variables[i]));
		if (fv.length)
			this._params.flashvars = fv.join('&');
		if (_isIE) {
			html.push('<object width="', this._width,
				'" height="', this._height,
				'" id="', this._id ,'"');
			if (this._class.length)
				html.push(' class="', this._class.join(' ') ,'"');
			html.push(' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
			html.push('<param name="src" value="', this._src, '"/>');
			for (i in this._params)
				html.push('<param name="', i, '" value="', _htmlChars(this._params[i]), '"/>');
			html.push('</object>');
		} else {
			html.push('<embed width="', this._width,
				'" height="', this._height,
				'" src="', this._src,
				'" id="', this._id, '"');
			if (this._class.length)
				html.push(' class="', this._class.join(' ') ,'"');
			for (i in this._params)
				html.push(' ', i, '="', _htmlChars(this._params[i]), '"');
			html.push(' pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"/>');
		}
		return html.join('');
	},

	html: function() {
		if (this._chechVersion && !jswf.chechVersion(this._requiredVersion))
			return this._buildAlternative();
		return this._buildEmbed();
	},

	insert: function(node) {
		if (node === undefined) {
			document.write(this.html());
			return this;
		}
		if (typeof node == 'string') {
			var elm = document.getElementById(node);
			if (elm === null) {
				throw new Error('Element id=' + node + ' not found');
			} else {
				node = elm;
			}
		}
		if (typeof node == 'object' && node.nodeType == 1) {
			node.innerHTML = this.html();
			return this;
		} else {
			throw new Error('Bed argument: ' + node.toString());
		}

	},

	id: function(v) {
		if (v === undefined) return this._id;
		this._id = v.toSting();
		return this;
	},
	addClass: function(v) {
		if (typeof v == 'string') v = v.split(',');
		for (var i = 0; i < v.length; i++) this._class.push(v[i]);
		return this;
	},
	getClass: function(v) {
		return this._class;
	},
	removeClass: function(v) {
		this._class = [];
		return this;
	},
	param: function(n, v) {
		if (n === undefined) return this._params;
		if (v === undefined) for (var i in n) this._params[i] = n[i];
		else this._params[n] = v;
		return this;
	},
	variable: function(n, v) {
		if (n === undefined) return this._variables;
		if (v === undefined) for (var i in n) this._variables[i] = n[i];
		else this._variables[n] = v;
		return this;
	},
	chechVersion: function(v) {
		this._chechVersion = !!v;
		return this;
	},
	requiredVersion: function(v) {
		if (v === undefined) return this._requiredVersion;
		if (typeof v == 'string') v = v.split('.', 3);
		this._requiredVersion = [];
		for (var i = 0; i < 3; i++) this._requiredVersion[i] = parseInt(v[0]) || 0;
		return this;
	}
};


jswf.setAlternative = function(v) {
	_default.alternative = v.toString();
};
jswf.getAlternative = function(v) {
	return _default.alternative;
};

jswf.setRequiredVersion = function(v) {
	_default.requiredVersion = [];
	if (typeof v == 'string') v = v.split('.', 3);
	for (var i = 0; i < 3; i++) _default._requiredVersion[i] = parseInt(v[0]) || 0;
};

var _v;
jswf.getVersion = function() {
	if (_v !== undefined) return _v;
	_v = null;
	var np = navigator.plugins;
	var ua = navigator.userAgent;
	if (np != null && np.length > 0) {
		var d = false;
		if (np['Shockwave Flash']) d = np['Shockwave Flash'].description;
		else if (np['Shockwave Flash 2.0']) d = np['Shockwave Flash 2.0'].description;
		if (d) {
			d = d.split(' ');
			var v = d[2].split('.');
			var vr = d[3];
			if (vr == '') vr = d[4];
			if (vr[0] == 'd') {
				vr = vr.substring(1);
			} else if (vr[0] == 'r') {
				vr = vr.substring(1);
				if (vr.indexOf('d') > 0) vr = vr.substring(0, vr.indexOf('d'));
			}
			_v = [parseInt(v[0]), parseInt(v[1]), parseInt(vr)];
			v = vr = null;
		}
		d = null;
	}
	else if (ua.toLowerCase().indexOf("webtv/2.6") != -1) _v = [4,0,0];
	else if (ua.toLowerCase().indexOf("webtv/2.5") != -1) _v = [3,0,0];
	else if (ua.toLowerCase().indexOf("webtv") != -1) _v = [2,0,0];
	else if (_isIE) {
		var v = false, a, e;
		try {
			a = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
			v = a.GetVariable('$version');
		} catch (e) { try {
			a = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			v = 'WIN 6,0,21,0';
			a.AllowScriptAccess = 'always';
			v = a.GetVariable('$version');
		} catch (e) { try {
			a = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.3');
			v = axo.GetVariable('$version');
		} catch (e) { try {
			a = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.3');
			v = 'WIN 3,0,18,0';
		} catch (e) { try {
			a = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			v = 'WIN 2,0,0,11';
		} catch (e) {}
		}}}}
		if (v) {
			v = v.split(' ')[1].split(",");
			_v = [parseInt(v[0]), parseInt(v[1]), parseInt(v[2])];
		}
		v = a = e = null;
	}
	np = ua = null;
	return _v;
};

jswf.chechVersion = function(v) {
	if (!v) v = _default.requiredVersion;
	var r = [], pv = jswf.getVersion();
	if (!pv) return false;
	if (typeof v == 'string') v = v.split('.', 3);
	for (var i = 0; i < 3; i++) r[i] = parseInt(v[i]) || 0;
	if (pv[0] > r[0]) return true;
	else if (pv[0] == r[0]) {
		if (pv[1] > r[1]) return true;
		else if (pv[1] == r[1] && pv[2] >= r[2]) return true;
	}
	return false;
};

var _isIE = (navigator.appVersion.indexOf("MSIE") != -1)
	&& (navigator.appVersion.toLowerCase().indexOf("win") != -1)
	&& !(navigator.userAgent.indexOf("Opera") != -1);

var _htmlChars = function(s) {
	return s.replace('"', '&quot;').replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;');
};

var _urlEncode = function(s) {
	s = s.toString().replace(/\r\n/g, '\n');
	var u = '';
	for (var n = 0; n < s.length; n++) {
		var c = s.charCodeAt(n);
		if (c < 128) {
			u += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			u += String.fromCharCode((c >> 6) | 192);
			u += String.fromCharCode((c & 63) | 128);
		} else {
			u += String.fromCharCode((c >> 12) | 224);
			u += String.fromCharCode(((c >> 6) & 63) | 128);
			u += String.fromCharCode((c & 63) | 128);
		}
	}
	return escape(u);
};

})();